#include "addnewdevice.h"
#include "ui_addnewdevice.h"

#include <QMessageBox>

AddNewDevice::AddNewDevice(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::AddNewDevice)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::CustomizeWindowHint|Qt::WindowCloseButtonHint);
    ui->deviceName->setPlaceholderText("请输入设备名称");
    ui->serialNumber->setPlaceholderText("请输入序列号");
    ui->Port->setPlaceholderText("请输入端口号");
    ui->MACAddr->setPlaceholderText("请输入MAC地址");

    /* 初始化 */
    cusBox = new CustomMessageBox();

    /* 限制文本框输入限制 */
    /* 设备名称 */
    QRegExp deviceNamerx("(\\w|-){1,50}");
    QRegExpValidator *deviceNameReg = new QRegExpValidator(deviceNamerx, this);
    ui->deviceName->setValidator(deviceNameReg);
    /* 序列号 */
    QRegExp serialrx("([a-z]|[A-Z]|[0-9]){1,30}");
    QRegExpValidator *serialReg = new QRegExpValidator(serialrx, this);
    ui->serialNumber->setValidator(serialReg);
    /* MAC地址 */
    QRegExp macrx("^[A-F0-9]{2}(-[A-F0-9]{2}){5}$|^[A-F0-9]{2}(:[A-F0-9]{2}){5}$");
    QRegExpValidator *macReg = new QRegExpValidator(macrx, this);
    ui->MACAddr->setValidator(macReg);
    /* 端口 */
    QRegExp portrx("^([1-9](\\d{0,3}))$|^([1-5]\\d{4})$|^(6[0-4]\\d{3})$|^(65[0-4]\\d{2})$|^(655[0-2]\\d)$|^(6553[0-5])$");
    QRegExpValidator *portReg = new QRegExpValidator(portrx, this);
    ui->Port->setValidator(portReg);

    /* ip和端口 数据校验：只能输入数字 */
    ui->IPAddr_1->setValidator(new QIntValidator(0, 255, this));
    ui->IPAddr_2->setValidator(new QIntValidator(0, 255, this));
    ui->IPAddr_3->setValidator(new QIntValidator(0, 255, this));
    ui->IPAddr_4->setValidator(new QIntValidator(0, 255, this));

    connect(ui->ok, &QPushButton::clicked, this, &AddNewDevice::getNewDeviceInfo);
    connect(ui->cancel, &QPushButton::clicked, this, [=](){
        ui->deviceName->clear();
        ui->serialNumber->clear();
        ui->IPAddr_1->clear();
        ui->IPAddr_2->clear();
        ui->IPAddr_3->clear();
        ui->IPAddr_4->clear();
        ui->Port->clear();
        ui->MACAddr->clear();
        this->close();
    });

    /* 输入校验：范围限制 */
    connect(ui->IPAddr_1, &QLineEdit::textChanged, this, [=](QString text){
        setDataValidator(ui->IPAddr_1, text, 255);
    });
    connect(ui->IPAddr_2, &QLineEdit::textChanged, this, [=](QString text){
        setDataValidator(ui->IPAddr_2, text, 255);
    });
    connect(ui->IPAddr_3, &QLineEdit::textChanged, this, [=](QString text){
        setDataValidator(ui->IPAddr_3, text, 255);
    });
    connect(ui->IPAddr_4, &QLineEdit::textChanged, this, [=](QString text){
        setDataValidator(ui->IPAddr_4, text, 255);
    });

}

AddNewDevice::~AddNewDevice()
{
    delete ui;
}

/* 获取新设备信息 */
void AddNewDevice::getNewDeviceInfo(){
    /* 内容验证 */
    if(ui->deviceName->text() == ""){
        cusBox->show(this, CMsg::CWarning, "警告", "设备名不能为空！");
        return;
    }
    else if(ui->serialNumber->text() == ""){
        cusBox->show(this, CMsg::CWarning, "警告", "序列号不能为空");
        return;
    }
    else if(ui->IPAddr_1->text() == "" || ui->IPAddr_2->text() == "" || ui->IPAddr_3->text() == "" || ui->IPAddr_4->text() == ""){
        cusBox->show(this, CMsg::CWarning, "警告", "IP地址不能为空！");
        return;
    }
    else if(ui->Port->text() == ""){
        cusBox->show(this, CMsg::CWarning, "警告", "端口号不能为空！");
        return;
    }
    else if(ui->MACAddr->text() == ""){
        cusBox->show(this, CMsg::CWarning, "警告", "MAC地址不能为空！");
        return;
    }

    QStringList macList = ui->MACAddr->displayText().split("-");
    QString ipAddr = ui->IPAddr_1->text() + "." + ui->IPAddr_2->text() + "." + ui->IPAddr_3->text() + "." + ui->IPAddr_4->text();

    /* 判断MAC地址位数是否正确 */
    if(macList.size() != 6){
        cusBox->show(this, CMsg::CWarning, "警告", "MAC地址错误");
        return;
    }else {
        foreach (QString unitMac, macList) {
            if(unitMac.length() != 2){
                cusBox->show(this, CMsg::CWarning, "警告", "MAC地址错误");
                return;
            }
        }
    }

    /* 写入新设备信息到结构体 */
    this->deviceInfo.deviceName = ui->deviceName->displayText();
    this->deviceInfo.MACAddr = ui->MACAddr->displayText();
    this->deviceInfo.IPAddr = ipAddr;
    this->deviceInfo.Port = ui->Port->displayText();
    this->deviceInfo.serialNumber = ui->serialNumber->displayText();
    this->deviceInfo.deviceStatus = "未连接";

    /* 判断是否有设备信息重复 */
    for(int i = 0; i < connectDeviceInfo.size(); i++){
        /* 设备名称重复 */
        if(connectDeviceInfo[i].dev.name == ui->deviceName->text()){
            cusBox->show(this, CMsg::CWarning, "设备列表", "设备名称已存在！");
            return;
        }
        /* 序列号重复 */
        if(connectDeviceInfo[i].dev.serial_number == ui->serialNumber->text()){
            cusBox->show(this, CMsg::CWarning, "设备列表", "设备序列号已存在！");
            return;
        }
        /* 设备IP重复 */
        QString ip = ui->IPAddr_1->displayText() + "." + ui->IPAddr_2->displayText() + "." + ui->IPAddr_3->displayText() + "." + ui->IPAddr_4->displayText();
        if(connectDeviceInfo[i].dev.ip == ip){
//            cusBox->show(this, CMsg::CWarning, "设备列表", "设备IP已存在！");
//            return;
        }
        /* 设备端口重复 */
        if(connectDeviceInfo[i].dev.port == ui->Port->text()){
            cusBox->show(this, CMsg::CWarning, "设备列表", "设备端口号已存在！");
            return;
        }
        /* 设备MAC重复 */
        if(connectDeviceInfo[i].dev.mac == ui->MACAddr->text()){
            cusBox->show(this, CMsg::CWarning, "设备列表", "设备MAC地址已存在！");
            return;
        }

    }

    emit newDeviceInfo(this->deviceInfo);
    this->close();
}

/* 数据校验 */
void AddNewDevice::setDataValidator(QLineEdit *obj, QString text, int max){
    if(text.toInt() > max)
        obj->setText(QString::number(max));
    if(text == "")
        obj->setText("0");
    if(text.toInt() >= 0 && text.toInt() < max){
        text = QString::number(0 + text.toInt());
        obj->setText(text);
    }
}

/* 页面关闭事件 */
void AddNewDevice::closeEvent(QCloseEvent *){
    /* 清空所有输入框 */
    ui->serialNumber->clear();
    ui->IPAddr_1->clear();
    ui->IPAddr_2->clear();
    ui->IPAddr_3->clear();
    ui->IPAddr_4->clear();
    ui->Port->clear();
    ui->deviceName->clear();
    ui->MACAddr->clear();
}
